package com.mallcloud.mall.mall.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mallcloud.mall.common.core.util.R;
import com.mallcloud.mall.common.excel.annotation.ResponseExcel;
import com.mallcloud.mall.mall.api.entity.MallPointsProductModel;
import com.mallcloud.mall.mall.api.entity.MallPointsProductViewModel;
import com.mallcloud.mall.mall.service.MallPointsProductService;
import com.mallcloud.mall.mall.service.MallPointsProductViewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "mall-积分商品")
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/mall/pointsProduct")
public class MallPointsProductController {
	private final MallPointsProductViewService pointProductViewService;
	private final MallPointsProductService pointProductService;

	private LambdaQueryWrapper<MallPointsProductViewModel> query(MallPointsProductViewModel pointProductViewModel){
		return new LambdaQueryWrapper<MallPointsProductViewModel>()
				.eq(StrUtil.isNotBlank(pointProductViewModel.getName()), MallPointsProductViewModel::getName, pointProductViewModel.getName())
				.eq(StrUtil.isNotBlank(pointProductViewModel.getStatus()), MallPointsProductViewModel::getStatus, pointProductViewModel.getStatus());
	}

	@ApiOperation("分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('mall_pointsproduct_view')")
	public R<IPage<MallPointsProductViewModel>> page(Page page, MallPointsProductViewModel pointProductViewModel) {
		return R.ok(pointProductViewService.page(page, query(pointProductViewModel)));
	}

	@ApiOperation("列表查询")
	@GetMapping("/list")
	@PreAuthorize("@pms.hasPermission('mall_pointsproduct_view')")
	public R<List<MallPointsProductViewModel>> list(MallPointsProductViewModel pointProductViewModel) {
		return R.ok(pointProductViewService.list(query(pointProductViewModel)));
	}

	@ApiOperation("下拉列表")
	@GetMapping("/select")
	@PreAuthorize("@pms.hasPermission('mall_pointsproduct_view')")
	public R<List<MallPointsProductViewModel>> options(MallPointsProductViewModel model) {
		return R.ok(pointProductViewService.options(model));
	}

	@ApiOperation("查询详情")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('mall_pointsproduct_view')")
	public R<MallPointsProductViewModel> getById(@PathVariable("id") String id) {
		return R.ok(pointProductViewService.getById(id));
	}

	@ApiOperation("查询详情")
	@GetMapping("/get-info")
	@PreAuthorize("@pms.hasPermission('mall_pointsproduct_view')")
	public R<MallPointsProductViewModel> getById(MallPointsProductViewModel entity) {
		return R.ok(pointProductViewService.getOne(Wrappers.query(entity)));
	}

	@ApiOperation("新增")
	@PostMapping("/save")
	@PreAuthorize("@pms.hasPermission('mall_pointsproduct_add')")
	public R<Boolean> save(@RequestBody MallPointsProductModel pointProduct) {
		return R.ok(pointProductService.savePointsProduct(pointProduct));
	}

	@ApiOperation("修改")
	@PostMapping("/update")
	@PreAuthorize("@pms.hasPermission('mall_pointsproduct_edit')")
	public R<Boolean> updateById(@RequestBody MallPointsProductModel pointProduct) {
		return R.ok(pointProductService.saveOrUpdate(pointProduct));
	}

	@ApiOperation("删除")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('mall_pointsproduct_del')")
	public R<Boolean> removeById(@PathVariable("id") String id) {
		return R.ok(pointProductService.removePointsProduct(id));
	}

	@ApiOperation("批量删除")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('mall_pointsproduct_del')")
	public R<Boolean> removeById(@RequestBody List<String> ids) {
		return R.ok(pointProductService.removePointsProducts(ids));
	}

	@ApiOperation("导出")
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('mall_pointsproduct_export')")
	public List<MallPointsProductViewModel> export(MallPointsProductViewModel pointProductViewModel) {
		return pointProductViewService.list(query(pointProductViewModel));
	}
}
